package com.twu.biblioteca;

import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    public static List<Book> bookList = Library.books;
    public static List<Movie> movieList = Library.movies;
    public static List<User> userList = Library.users;

    static User currentUser = new User("", "", "", "", "");
    static User userLoggedIn = new User("", "", "", "", "");

    public static void main(String[] args) {

        int menuOptionSelected;

        welcome();

        while (true) {
            menuOptions();
            menuOptionSelected = chooseMenuOption();

            switch (menuOptionSelected) {
                case 1:
                    displayBookList(bookList);
                    break;
                case 2:
                    viewMovieListing();
                    break;
                case 3:
                    userLoggedIn = login();
                    break;
                case 4:
                    reserveBook();
                    break;
                case 5:
                    membershipDetails();
                    break;
                case 6:
                    logout();
                    break;
                case 7:
                    System.exit(0);
            }
        }
    }

    public static void welcome() {

        System.out.print("Welcome to Bangalore Public Library\n");
    }

    public static void menuOptions() {
        System.out.print("Library Menu:\n");
        System.out.print("1. View list of books \n" +
                "2. View list of movies\n" +
                "3. Login\n" +
                "4. Reserve a book\n" +
                "5. Check membership details\n" +
                "6. Logout\n" +
                "7. Exit\n");
    }

    public static void displayBookList(List<Book> bookList) {

        System.out.print("ID. Name\n");

        for (Book aBookList : bookList) {
            Book.displayBookInformation(aBookList);
        }
    }

    public static int chooseMenuOption() {
        int choice;

        System.out.println("\nPlease select an option from the above menu:");
        choice = getUserChoice();

        while (choice > 7 || choice < 0) {
            System.out.println(invalidOption());
            menuOptions();
            choice = getUserChoice();
        }
        return choice;
    }

    public static int getUserChoice() {
        Scanner userChoice = new Scanner(System.in);
        return userChoice.nextInt();
    }

    public static void reserveBook() {

        if (userLoggedIn.loggedIn == false) {
            System.out.println("\nPlease Login to reserve a book");
            return;
        }

        int bookNumber;
        String reservationStatus;

        displayBookList(bookList);
        System.out.println("\nEnter the bookID of the book you want to reserve:");
        bookNumber = getUserChoice();
        reservationStatus = checkIfBookAvailable(bookNumber);
        System.out.println(reservationStatus);
    }

    public static User login() {
        logout();   /* To logout any existing user. */

        LoginCredentials loginCredentials = new LoginCredentials().invoke();
        String UserIp = loginCredentials.getUserIp();
        String passwordGiven = loginCredentials.getPasswordGiven();

        return findUserWithTheGivenUsernameAndPassword(UserIp, passwordGiven, userList);

    }

    public static User findUserWithTheGivenUsernameAndPassword(String userIp, String passwordGiven,List<User> userList ) {
        User tempUser;

        for (User anUserList : userList) {
            tempUser = anUserList;

            if (checkUsernamePasswordCombination(userIp, passwordGiven, tempUser)) {
                return currentUser;
            }
        }

        System.out.println("\nLogin not successful");
        return currentUser;
    }

    private static boolean checkUsernamePasswordCombination(String userIp, String passwordGiven, User tempuser) {

        if (tempuser.libraryID.equals(userIp) && passwordGiven.equals(tempuser.password)) {
            currentUser = tempuser;
            currentUser.loggedIn = true;
            System.out.println("\nLogin successful");
            return true;
        }

        return false;
    }

    public static void logout() {

        for (int i = 0; i < userList.size(); i++) {
            User tempUser = userList.get(i);
            tempUser.loggedIn = false;
        }
    }

    public static String checkIfBookAvailable(int bookNumber) {
        /* Assumption: User enters a valid book ID */

        for (Book book : bookList) {

            if (checkIfBookIsReserved(bookNumber, book)) {
                return (bookReservationSuccessful());
            }
        }
        return (bookReservationUnsuccessful());
    }

    public static boolean checkIfBookIsReserved(int bookNumber, Book book) {

        if ((bookNumber == book.bookID) && (book.whetherReserved == false)) {
            book.whetherReserved = true;
            return true;
        }
        return false;
    }

    private static void viewMovieListing() {

        System.out.println(Movie.displayHeading());

        for (int i = 0; i < movieList.size(); i++) {
            Movie.displayMovieInformation(movieList.get(i));
        }
    }

    public static String invalidOption() {
        return ("Select a valid option!!");
    }

    public static String bookReservationSuccessful() {
        return ("Thank You! Enjoy the book.");
    }

    public static String bookReservationUnsuccessful() {
        return ("Sorry we don't have that book yet.");
    }

    public static void membershipDetails() {

        if (userLoggedIn.loggedIn == false) {
            System.out.print("Please talk to a Librarian. Thank you.");
            return;
        }

        User.getMembershipDetails(userLoggedIn);
    }


    private static class LoginCredentials {
        private String userIp;
        private String passwordGiven;

        public String getUserIp() {
            return userIp;
        }

        public String getPasswordGiven() {
            return passwordGiven;
        }

        private void setPasswordGiven(String passwordGiven) {
            this.passwordGiven = passwordGiven;
        }

        public LoginCredentials invoke() {

            Scanner input = new Scanner(System.in);

            System.out.println("\nPlease enter your user ID:");
            setUserIp (input.next());

            System.out.println("\nEnter your password:");
            setPasswordGiven (input.next());
            return this;
        }

        private void setUserIp(String userIp) {
            this.userIp = userIp;
        }
    }
}